import time

from selenium.webdriver.support.ui import WebDriverWait

class Base():
    # 浏览器驱动
    def __init__(self,driver):
        self.driver=driver
    # 打开网址
    def open_url(self,url):
        self.driver.get(url)
    # 退出
    def quit(self):
        self.driver.quit()

    def find_element(self,loc):
        try:
           #  显式等待
           WebDriverWait(self.driver,20,0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
           return self.driver.find_element(*loc)
        except Exception as e:
            t=time.strftime('%Y%m%d-%H%M%S')
            self.driver.save_screenshot(t+'.png')
            print(e)

    # 输入文本
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    # 点击
    def click(self,loc):
        self.find_element(loc).click()
    # 文本
    def text(self,loc):
        return self.find_element(loc).text
    # 滚动底部
    def scrollTo(self):
        self.driver.execute_script('window(0,scrollTo.document.body.scrollHeight)')

